package com.example.plumneedle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.dalimao.corelibrary.VerificationCodeInput;
import com.example.plumneedle.utils.CodeUtils;
import com.mob.MobSDK;

import org.json.JSONException;
import org.json.JSONObject;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class Resiter extends AppCompatActivity {
    Button button;
    class Mhandler extends Handler {
        // 通过复写handlerMessage() 从而确定更新UI的操作
        @Override
        public void handleMessage(Message msg) {
            // 根据不同线程发送过来的消息，执行不同的UI操作
            // 根据 Message对象的what属性 标识不同的消息
            switch (msg.what) {
                case 1:
                    Log.i("Resiter.class", "msg="+(String) msg.obj);
                    button.setText((String)msg.obj);
                   // mTextView.setText("执行了线程1的UI操作");
                    break;
                case 2:
                   // mTextView.setText("执行了线程2的UI操作");
                    break;
            }
        }
    }
    private Bitmap bitmap;
    private String code;
    EditText editText;
    EventHandler handler;
    Mhandler mHandler;
    VerificationCodeInput verificationCodeInput;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resiter);
        MobSDK.init(this, "30758f83a16a2","9a26dba5151c666c0e99069902791c81");
        button=findViewById(R.id.button);
        verificationCodeInput=findViewById(R.id.dalimao);
        mHandler = new Mhandler();
        verificationCodeInput.setOnCompleteListener(new VerificationCodeInput.Listener() {
            @Override
            public void onComplete(String content) {
                Toast.makeText(Resiter.this,content,Toast.LENGTH_SHORT).show();
                Message msg = Message.obtain();
                msg.what = 1; // 消息标识
                msg.obj =content; // 消息内存存放

                // 步骤4：在工作线程中 通过Handler发送消息到消息队列中
                mHandler.sendMessage(msg);
            }
        });
        editText=findViewById(R.id.et_forgetPass_PhoneNum);
        final ImageView image = (ImageView) findViewById(R.id.image);
        //获取工具类生成的图片验证码对象
        bitmap = CodeUtils.getInstance().createBitmap();
        //获取当前图片验证码的对应内容用于校验
        code = CodeUtils.getInstance().getCode();

        image.setImageBitmap(bitmap);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bitmap = CodeUtils.getInstance().createBitmap();
                code = CodeUtils.getInstance().getCode();
                image.setImageBitmap(bitmap);
                Toast.makeText(Resiter.this,code,Toast.LENGTH_SHORT).show();
            }
        });
        handler = new EventHandler(){
            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE){
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(Resiter.this,"验证成功",Toast.LENGTH_SHORT).show();
                                Button button=findViewById(R.id.but_forgetpass_toSetCodes);
                                button.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent=new Intent(Resiter.this,Resiter1.class);
                                        intent.putExtra("phone",editText.getText().toString());
                                        startActivity(intent);
                                    }
                                });
                            }
                        });

                    }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                        //获取验证码成功
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(Resiter.this,"验证码已发送",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                    }
                }else{
                    ((Throwable)data).printStackTrace();
                    Throwable throwable = (Throwable) data;
                    try {
                        JSONObject obj = new JSONObject(throwable.getMessage());
                        final String des = obj.optString("detail");
                        if (!TextUtils.isEmpty(des)){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(Resiter.this,"提交错误信息", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }
        };

        SMSSDK.registerEventHandler(handler);
    }
    public void tijiao(View view) {
        String number = button.getText().toString();
        String phone=editText.getText().toString();
        Log.i("Resiter.class","msg1="+number);
        SMSSDK.submitVerificationCode("86",phone,number);
    }
    public void play(View view) {
        //获取验证码
        String phone=editText.getText().toString();
        SMSSDK.getVerificationCode("86",phone);
    }
}
